// pages/freeRideHome/freeRideHome.js
//引用
var fileData = require('../../utils/data.js');
var main = require('../../utils/main.js');
const util = require('../../utils/util.js')
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    userInfo: '',
    startPlace: '',
    termini: '',
    date: '',
    seachType: 0,
    banner_url: [],
    bottabData: app.globalData.bottabDataForSfc,
    interval: 3000,
    duration: 1000,
    vertical: false,
    indicatordots: true,
    autoplay: true,
    indicatored: '#F08631',
    indicator: '#fff',
    imageUrl: app.globalData.imageUrl,
    array: ['全部', '车找人', '人找车', '货找车', '车找货'],
    navTopItems: [
      { id: 1, icon: app.globalData.imageUrl + 'ic_downwind@2x.png', title: '人找车', url: '/pages/personForCar/personForCar?type=2' },
      { id: 2, icon: app.globalData.imageUrl + 'ic_car_for_someone@2x.png', title: '车找人', url: '/pages/personForCar/personForCar?type=1' },
      { id: 3, icon: app.globalData.imageUrl + 'ic_for_the_car@2x.png', title: '货找车', url: '/pages/personForCar/personForCar?type=3' },
      { id: 4, icon: app.globalData.imageUrl + 'ic_the_car_to_find_goods@2x.png', title: '车找货', url: '/pages/personForCar/personForCar?type=4' }
    ],
    curNavId: 1,
    curIndex: 0,
    colors: ["green", "yellow", "bule", "red"],
    focus: '',
    advert_url: fileData.getAdvertData(),
    actiItem: [],
    webviewurl: '',
    bannerAdvert: [],
    page: 1,
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: false,
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    main.changeInput.apply(this, []);
    fileData.getAdvertData.apply(this, ['1', 'advert_url']);
    fileData.getBannerData.apply(this, ['1', 'top', 0,'banner_url']);
    fileData.getBannerData.apply(this, ['1', 'top', 1, 'bannerAdvert']);
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['bottabData[0].current']: true
    })
    main.changeInput.apply(this, []);
    wx.showLoading({})
  },
  enterDesk: function () {
    wx.navigateTo({
      url: '/pages/infoDesk/infoDesk?index=1',
    })
  },
  // 广告详情页面
  enterDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../pages/detail/detail?id=' + id,
    })
  },
  reversal: function (e) {
    this.setData({
      startPlace: this.data.termini,
      termini: this.data.startPlace
    })
  },
  switchBotab(e) {
    if (e.currentTarget.dataset.index != 0) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }
  },
  enterPerson:function(e){
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id=' + e.target.id
    })
  },
  bindDateChange: function (e) {//时间选择改变事件
    this.setData({
      date: e.detail.value
    })
  },
  bindPickerChange: function (e) {//时间选择改变事件
    this.setData({
      seachType: e.detail.value
    })
    // if (e.detail.value == 0){
    //   this.getList(true);
    // }else{
    // this.getSeachData(true);
    // }
  },
  loadAdvert() {//跳转广告链接
    // wx.request({
    //   url: 'https://mp.weixin.qq.com/',  //服务器参数接收地址，  
    //   // data: { token: token },
    //   method: 'GET',
    //   success: function (res) {
    //     console.log(res.data)        //打印获取数据  
    //     // that.setData({
    //     //   duanziDetail: res.data.result
    //     // })
    //   } 
    // }) 
  },
  getSeachData(flag) {
    var obj = { '1': '车找人', '2': '人找车', '3': '货找车', '4': '车找货' };
    var begintime_end = '';
    if (this.data.date != '') {
      begintime_end = util.getNow();
    }
    var _this = this;
    var param = {
      category:1,
      currentPage: this.data.page,
      pageNumber: '10',
      placeFrom: this.data.startPlace,
      placeTo: this.data.termini,
      backupA: this.data.date,
      // orderStatus:1
      // endtime: begintime_end
    };
    if (_this.data.seachType!=0){
      param.type = _this.data.seachType;
    }
    
    wx.request({
      url: app.globalData.httpApiCar + 'freeRideInfo/page', //仅为示例，并非真实的接口地址
      data: param,
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess:true
        })
        if (res.data.data.pp){
        if (res.data.data.pp.constructor == Array) {
          if (res.data.data.pp.length < 10) {
            _this.setData({
              hasData: false
            })
          } else {
            _this.setData({
              hasData: true
            })
          }
          res.data.data.pp.forEach(function (ele) {
            ele.isStart = false;//未出发
            if (new Date().getTime() > new Date(ele.backupA.replace(/-/g, "/")).getTime()){
              ele.isStart=true;
            }
            if (ele.createTime) {
              ele.time = util.getDays(ele.createTime)
            }
            if (ele.labelArray != undefined && ele.labelArray != '') {
              ele.mark = ele.labelArray.split('/');
            } else {
              ele.mark = [];
            }
          })
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            _this.setData({
              actiItem: []
            });
          } else {
            _this.setData({
              loadMore: false
            });
          }
          _this.data.actiItem = _this.data.actiItem.concat(res.data.data.pp);
          _this.setData({
            actiItem: _this.data.actiItem,
            hasData: _this.data.hasData
          })
        }
      }
      }
    })
    if (this.data.startPlace != '') {
      this.addHistory(this.data.startPlace);
    }
    if (this.data.termini != '') {
      this.addHistory(this.data.termini);
    }
  },
  addHistory:function(value){
    wx.request({
      url: app.globalData.httpApiCar + 'searchInfo/save',
      data: {
        userId: this.data.userInfo.id,
        serachName: value,
        type: 1,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
      }
    })
  },
  showDetail: function (e) {
    // wx.setStorage({
    //   key: 'carDetail',
    //   data: e.currentTarget.dataset.item
    // })
      wx.navigateTo({
        url: '/pages/carDetail/carDetail?id=' + e.currentTarget.dataset.item.id
      })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      loadSuccess:false
    })
    wx.showLoading()
    this.getSeachData(true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  switchTap: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page: 1
    })
    this.getSeachData(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasData) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        page: this.data.page + 1
      });
      this.getSeachData();
    }
  },
})